package forumTest;

import common.TestUtils;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-11-14
 * Time: 15:00
 */
// 注册页面
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumSignTest {
    private static WebDriver driver = null;
    @BeforeAll
    static void setUp() throws InterruptedException {
        // 初始化驱动对象
        driver = TestUtils.createDriver();
    }

    @AfterAll
    static void tearDown() {
        // 关闭驱动
        driver.quit();
    }

    // 注册成功
    @Order(1)
    @ParameterizedTest
    @CsvSource({
        "testSign1,test1,123456,123456,white",
        "testSign2,test2,456789,456789,white"
    })
    void successedSign(String userName,String nickName,String password,String repeatPassword,String code) throws InterruptedException {
        driver.get("http://118.25.26.147:58080/sign-up.html");
        // 访问注册界面（观察是否正常访问）
        // 注册操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(repeatPassword);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        driver.findElement(By.cssSelector("#policy")).click();
        // 点击注册按钮
        driver.findElement(By.cssSelector("#submit")).click();
        // 观察是否跳转到登录页
    }

    // 注册失败
    // 缺少表单项
    @Order(2)
    @ParameterizedTest
    @CsvSource({"test,123,123,code"})
    void failedSign1(String userName,String password,String repeatPassword,String code) throws InterruptedException {
        System.out.println(userName);
        driver.get("http://118.25.26.147:58080/sign-up.html");
        // 注册操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(repeatPassword);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        driver.findElement(By.cssSelector("#policy")).click();
        // 点击注册按钮
        driver.findElement(By.cssSelector("#submit")).click();

        String text = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div")).getText();
        Assertions.assertEquals("昵称不能为空",text);
    }

    // 两次输入密码不一致
    @Order(3)
    @ParameterizedTest
    @CsvSource({"test,test,123,1234,code"})
    void failedSign2(String userName,String nickName,String password,String repeatPassword,String code) throws InterruptedException {
        driver.get("http://118.25.26.147:58080/sign-up.html");
        // 注册操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(repeatPassword);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        driver.findElement(By.cssSelector("#policy")).click();
        // 点击注册按钮
        driver.findElement(By.cssSelector("#submit")).click();

        String text = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",text);
    }

    // 用户名已存在
    @Order(4)
    @ParameterizedTest
    @CsvSource({"testSign1,test,123,123,code"})
    void failedSign3(String userName,String nickName,String password,String repeatPassword,String code) throws InterruptedException {
        driver.get("http://118.25.26.147:58080/sign-up.html");
        // 注册操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(repeatPassword);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        driver.findElement(By.cssSelector("#policy")).click();
        // 点击注册按钮
        driver.findElement(By.cssSelector("#submit")).click();


        String text = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        Assertions.assertTrue(text.contains("用户已存在"));
    }

    // 验证码错误
    @Order(5)
    @ParameterizedTest
    @CsvSource({"testA,testA,123,123,code"})
    void failedSign4(String userName,String nickName,String password,String repeatPassword,String code) throws InterruptedException {
        driver.get("http://118.25.26.147:58080/sign-up.html");
        // 注册操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(repeatPassword);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        driver.findElement(By.cssSelector("#policy")).click();
        // 点击注册按钮
        driver.findElement(By.cssSelector("#submit")).click();

        String text = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        Assertions.assertTrue(text.contains("验证码校验失败，请重试!"));
    }

}
